package com.ruoyi.dev.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dev.mapper.DevApiMapper;
import com.ruoyi.dev.domain.DevApi;
import com.ruoyi.dev.service.IDevApiService;

/**
 * 接口信息Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-02-25
 */
@Service
public class DevApiServiceImpl implements IDevApiService 
{
    public static final String CACHE_MAP = "dev_api";
    private static final Logger log = LoggerFactory.getLogger(DevApiServiceImpl.class);

    @Autowired
    private DevApiMapper devApiMapper;
    @Autowired
    protected RedisCache redisCache;


    /**
     * 查询接口信息
     *
     * @param uri 接口地址
     * @return 接口信息
     */
    @Override
    public DevApi selectDevApiByUri(String uri){
        DevApi devApi = redisCache.getCacheObject(CACHE_MAP, uri);
        if(devApi == null){
            devApi = devApiMapper.selectDevApiByUri(uri);
            if(devApi != null){
                redisCache.setCacheObject(CACHE_MAP, uri, devApi);
            }
        }
        return devApi;
    }

    /**
     * 查询接口信息
     * 
     * @param id 接口信息主键
     * @return 接口信息
     */
    @Override
    public DevApi selectDevApiById(Long id)
    {
        return devApiMapper.selectDevApiById(id);
    }

    /**
     * 查询接口信息列表
     * 
     * @param devApi 接口信息
     * @return 接口信息
     */
    @Override
    public List<DevApi> selectDevApiList(DevApi devApi)
    {
        return devApiMapper.selectDevApiList(devApi);
    }

    /**
     * 新增接口信息
     * 
     * @param devApi 接口信息
     * @return 结果
     */
    @Override
    public int insertDevApi(DevApi devApi)
    {
        DevApi old = devApiMapper.selectDevApiByUri(devApi.getUri());
        if(old != null){
            throw new ServiceException("接口地址不能重复:"+devApi.getUri());
        }
        if(StringUtils.isEmpty(devApi.getSecret())) {
            devApi.setSecret(UUID.fastUUID().toString(true));
        }
        devApi.setCreateTime(DateUtils.getNowDate());
        return devApiMapper.insertDevApi(devApi);
    }

    /**
     * 修改接口信息
     * 
     * @param devApi 接口信息
     * @return 结果
     */
    @Override
    public int updateDevApi(DevApi devApi)
    {
        DevApi old = devApiMapper.selectDevApiByUri(devApi.getUri());
        if(old != null && !old.getId().equals(devApi.getId())){
            throw new ServiceException("接口地址不能重复:"+devApi.getUri());
        }
        if(StringUtils.isEmpty(devApi.getSecret())) {
            devApi.setSecret(UUID.fastUUID().toString(true));
        }
        devApi.setUpdateTime(DateUtils.getNowDate());
        return devApiMapper.updateDevApi(devApi);
    }

    /**
     * 批量删除接口信息
     * 
     * @param ids 需要删除的接口信息主键
     * @return 结果
     */
    @Override
    public int deleteDevApiByIds(Long[] ids)
    {
        return devApiMapper.deleteDevApiByIds(ids);
    }

    /**
     * 删除接口信息信息
     * 
     * @param id 接口信息主键
     * @return 结果
     */
    @Override
    public int deleteDevApiById(Long id)
    {
        return devApiMapper.deleteDevApiById(id);
    }


}
